package com.lebang.service;

import com.lebang.domain.MemberCashFastWithdrawal;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.entity.member.LbbMemberTrade;
import com.lebang.mapper.LbbMemberTradeMapper;
import com.lebang.mapper.MemberCashFastWithdrawalMapper;
import com.lebang.model.enums.ResultVo;
import com.lebang.model.enums.buss.RewardType;
import com.lebang.model.enums.buss.TradeCategory;
import com.lebang.model.enums.buss.TradeStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Service
public class BusWithdrawDepositService {
    @Autowired
    private MemberNoticeService memberNoticeService;
    @Autowired
    private MemberCashFastWithdrawalMapper fastWithdrawalMapper;
    @Autowired
    private LbbMemberTradeMapper lbbMemberTradeMapper;

    //提现记录
    public void creatFastWishCash(LbbMember member, Integer type, int amount, RequestHeaders requestHeaders, String tradeNo) {
        BigDecimal i = new BigDecimal(amount);
        i = i.divide(new BigDecimal(100), 2, BigDecimal.ROUND_DOWN);
        String desc = "您申请的{money}提现审核成功！";
        String money = i.toString();
        desc = desc.replace("{money}", money);
        memberNoticeService.addNoticeAndPush("提现通知", desc, member.getMemberId(), requestHeaders);
        LbbMemberTrade trade1 = new LbbMemberTrade();
        trade1.setCreateTime(new Date());
        trade1.setModifyTime(new Date());
        trade1.setHasDeleted("False");
        //标记是新版提现防止重复计算
        trade1.setOrderId("xxx");
        trade1.setMemberId(member.getMemberId());
        trade1.setAppId("xianyu");
        trade1.setAppStore(requestHeaders.getAppStore());
        trade1.setVersion(requestHeaders.getVersion());
        trade1.setTitle("提现-" + LocalDate.now().format(DateTimeFormatter.ISO_DATE));
        trade1.setAmount(amount);
        trade1.setExtra(0);
        trade1.setRemark(desc);
        trade1.setCategory(TradeCategory.Outlay);
        trade1.setType(RewardType.Cash);
        trade1.setStatus(TradeStatus.Success);
        trade1.setOs(requestHeaders.getOs());
        trade1.setOsv(requestHeaders.getOsv());
        trade1.setIpAddr(requestHeaders.getIpAddr());
        trade1.setMacAddr(requestHeaders.getMacAddr());
        trade1.setImei(requestHeaders.getImei());
        trade1.setTradeNo(tradeNo);
        trade1.setRemittance(2);
        trade1.setMethod(type);
        trade1.setNote(desc);
        trade1.setCheckStatus(3);
        trade1.setPayType(type);
        trade1.setTradeTime(new Date());
        trade1.setContent("");
        lbbMemberTradeMapper.insertSelective(trade1);
        MemberCashFastWithdrawal trade = new MemberCashFastWithdrawal();
        trade.setImei(requestHeaders.getImei());
        trade.setIpAddr(requestHeaders.getIpAddr());
        trade.setMacAddr(requestHeaders.getMacAddr());
        trade.setAmount(amount);
        trade.setAppStore(requestHeaders.getAppStore());
        trade.setMemberId(member.getMemberId());
        trade.setTitle("提现-" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE));
        trade.setVersion(requestHeaders.getVersion());
        trade.setCreateTime(LocalDateTime.now());
        trade.setPhone(member.getPhone());
        trade.setTradeAccount(member.getWxOpenid());
        trade.setRealName(member.getWeixinName());
        trade.setMethod(type);
        trade.setTradeNo(tradeNo);
        trade.setCheckStatus(3);
        trade.setPayType(type);
        fastWithdrawalMapper.insertSelective(trade);
    }
}
